package com.wanxi.dao.Impl;

import com.wanxi.dao.ContactDao;
import com.wanxi.model.Contact;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ContactDaoImpl implements ContactDao {
    @Override
    public List<Contact> findAllContact() {
        List<Contact> list = new ArrayList<>();
        String sql = "select * from contact";
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()) {
                list.add(Contact.builder()
                        .id(query.getInt("id"))
                        .userName(query.getString("userName"))
                        .phone(query.getString("phone"))
                        .email(query.getString("email"))
                        .content(query.getString("content"))
                        .enable(query.getInt("enable"))
                        .createTime(query.getTimestamp("createTime"))
                        .updateTime(query.getTimestamp("updateTime"))
                        .remark(query.getString("remark"))
                        .build());
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Contact findContactById(Contact contact) {
        String sql="select * from contact where id="+contact.getId();
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
                return Contact.builder()
                        .id(query.getInt("id"))
                        .userName(query.getString("userName"))
                        .phone(query.getString("phone"))
                        .email(query.getString("email"))
                        .content(query.getString("content"))
                        .enable(query.getInt("enable"))
                        .createTime(query.getTimestamp("createTime"))
                        .updateTime(query.getTimestamp("updateTime"))
                        .remark(query.getString("remark"))
                        .build();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public int updateEnable(Contact contact) {
        String sql="update contact set enable="+contact.getEnable()+",updateTime=(select now()) where id="+contact.getId();
        return JDBC.update(sql);
    }

    @Override
    public int addContact(Contact contact) {
        String sql="insert into contact(userName,email,phone,content,enable,createTime,updateTime,remark) "
                +"values('"+contact.getUserName()
                +"','"+ contact.getEmail()
                +"','"+ contact.getPhone()
                +"','"+ contact.getContent()
                +"',"+ contact.getEnable()
                +",(select now()),(select now()),'"+contact.getRemark()
                +"')";
        return JDBC.update(sql);
    }

    @Override
    public int deleteContact(Contact contact) {
        String sql="delete from contact where id="+contact.getId();
        return JDBC.update(sql);
    }

    @Override
    public int updateContact(Contact contact) {
        String sql="update contact set userName='"+contact.getUserName()
                +"',enable="+contact.getEnable()
                +",phone='"+contact.getPhone()
                +"',email='"+contact.getEmail()
                +"',content='"+contact.getContent()
                +"',remark='"+contact.getRemark()
                +"',updateTime=(select now()) where id="+contact.getId();
        return JDBC.update(sql);
    }
}
